#!/usr/bin/python

import time
import MySQLdb

host = "130.89.65.114"
port = 3306
user = "quest"
passwd = "quest"
dbs = "test"
outfile = "./sql.dat"

db = MySQLdb.connect (host = host, port=port, user = user, passwd = passwd, db = dbs)

print "Connecting to: %s at %s:%s (as '%s')" %(dbs, host, port, user)
print "-> Output: %s" %(outfile)
cursor = db.cursor()
sql = "SELECT * FROM jos" 
try:
	cursor.execute(sql)
	matrix = cursor.fetchall()
	print "-> Query check OK"
except:
	print "Error: query check failed"

while True:
	time.sleep(.1)  # seconds
	cursor = db.cursor()
	obj = [0]*100
	sql = "SELECT * FROM jos" 
	try:
		cursor.execute(sql)
		matrix = cursor.fetchall()
		FILE = open(outfile,"w")
		for line in matrix:
			FILE.write ("%s,%s,%s,%s,%s\n" %(line[0], line[1],line[2],line[3],line[4]))
			#print "%s,%s,%s,%s,%s\n" %(line[0], line[1],line[2],line[3],line[4])
		FILE.close()
	except:
		print "Error: unable to fetch data"
        db.rollback()


db.close()




